import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'drf2.settings'
import django
django.setup()
from aaa import models

x = list(models.Permission.objects.all().values('id', 'authName', 'url', 'pid_id', 'level'))

# 一级二级权限
fir_sen_dict = {}
third_list = []
for item in x:
    id = item['id']
    authName = item['authName']
    url = item['url']
    pid = item['pid_id']
    print('pid',pid,authName)
    level = item['level']
    if pid:
        if level == '2':
            third_list.append(item)
            print('third_list', third_list)
            continue
        # 二级权限设置children
        item['children'] = []
        if pid in fir_sen_dict:
            fir_sen_dict[pid]['children'].append(item)
        else:
            fir_sen_dict[pid] = {'children': [item]}
    else:
        # 过滤掉首页
        if id == 1:
            continue
        print('fir_sen_dict',fir_sen_dict)
        fir_sen_dict[id]['id'] = id
        fir_sen_dict[id]['authName'] = authName
        fir_sen_dict[id]['url'] = url


print(fir_sen_dict)